#!/usr/bin/env python2.7
#
# Copyright (C) 2013-2016 DNAnexus, Inc.
#
# This file is part of dx-toolkit (DNAnexus platform client libraries).
#
#   Licensed under the Apache License, Version 2.0 (the "License"); you may not
#   use this file except in compliance with the License. You may obtain a copy
#   of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#   License for the specific language governing permissions and limitations
#   under the License.

import sys, json

preamble = '''# Do not modify this file by hand.
#
# It is automatically generated by src/api_wrappers/generatePerlAPIWrappers.py.
# (Run make api_wrappers to update it.)

package DNAnexus::API;

use strict;
use Exporter;
use DNAnexus qw(DXHTTPRequest);
'''

postscript = '''
our @ISA = "Exporter";
our @EXPORT_OK = qw({all_method_names});
'''

class_method_template = '''
sub {method_name}(;$%) {{
    my ($input_params, %kwargs) = @_;
    %kwargs = () unless %kwargs;
    return DXHTTPRequest('{route}', $input_params, %kwargs);
}}
'''

object_method_template = '''
sub {method_name}($;$%) {{
    my ($object_id, $input_params, %kwargs) = @_;
    %kwargs = () unless %kwargs;
    return DXHTTPRequest('/'.$object_id.'/{method_route}', $input_params, %kwargs);
}}
'''

app_object_method_template = '''
sub {method_name}($;$%) {{
    my ($app_id_or_name, $input_params, %kwargs) = @_;
    %kwargs = () unless %kwargs;
    return DXHTTPRequest('/'.$app_id_or_name.'/{method_route}', $input_params, %kwargs);
}}

sub {method_name}WithAlias($;$%) {{
    my ($app_name, $app_alias, $input_params, %kwargs) = @_;
    %kwargs = () unless %kwargs;
    return {method_name}($app_name.'/'.$app_alias, $input_params, %kwargs);
}}
'''

print preamble

all_method_names = []

for method in json.loads(sys.stdin.read()):
    route, signature, opts = method
    method_name = signature.split("(")[0]
    if (opts['objectMethod']):
        root, oid_route, method_route = route.split("/")
        if oid_route == 'app-xxxx':
            print app_object_method_template.format(method_name=method_name, method_route=method_route)
        else:
            print object_method_template.format(method_name=method_name, method_route=method_route)
    else:
        print class_method_template.format(method_name=method_name, route=route)
    all_method_names.append(method_name)

print postscript.format(all_method_names=" ".join(all_method_names))
